//
//  SelfTools.m
//  SmartCommunity
//
//  Created by Mac on 15/7/25.
//  Copyright (c) 2015年 smartcommunity. All rights reserved.
//
#define NUMBERSPERIOD @"0123456789."
#import "SelfTools.h"
#include <sys/sysctl.h>
#import "AppDelegate.h"

@implementation SelfTools

#pragma mark 判断字符串是否为空
+(BOOL)isEmpty:(NSString *)str{
    if (str==nil || [str isEqual:[NSNull null]]) {
        return YES;
    }
    NSString *s=[str stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([s isEqualToStringAdd:@""]) {
        return YES;
    }
    return NO;

}


//获得设备型号
+ (NSString *)getCurrentDeviceModel
{
    int mib[2];
    size_t len;
    char *machine;
    
    mib[0] = CTL_HW;
    mib[1] = HW_MACHINE;
    sysctl(mib, 2, NULL, &len, NULL, 0);
    machine = malloc(len);
    sysctl(mib, 2, machine, &len, NULL, 0);
    
    NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
    free(machine);
    
    if ([platform isEqualToStringAdd:@"iPhone1,1"]) return @"iPhone 2G (A1203)";
    if ([platform isEqualToStringAdd:@"iPhone1,2"]) return @"iPhone 3G (A1241/A1324)";
    if ([platform isEqualToStringAdd:@"iPhone2,1"]) return @"iPhone 3GS (A1303/A1325)";
    if ([platform isEqualToStringAdd:@"iPhone3,1"]) return @"iPhone 4 (A1332)";
    if ([platform isEqualToStringAdd:@"iPhone3,2"]) return @"iPhone 4 (A1332)";
    if ([platform isEqualToStringAdd:@"iPhone3,3"]) return @"iPhone 4 (A1349)";
    if ([platform isEqualToStringAdd:@"iPhone4,1"]) return @"iPhone 4S (A1387/A1431)";
    if ([platform isEqualToStringAdd:@"iPhone5,1"]) return @"iPhone 5 (A1428)";
    if ([platform isEqualToStringAdd:@"iPhone5,2"]) return @"iPhone 5 (A1429/A1442)";
    if ([platform isEqualToStringAdd:@"iPhone5,3"]) return @"iPhone 5c (A1456/A1532)";
    if ([platform isEqualToStringAdd:@"iPhone5,4"]) return @"iPhone 5c (A1507/A1516/A1526/A1529)";
    if ([platform isEqualToStringAdd:@"iPhone6,1"]) return @"iPhone 5s (A1453/A1533)";
    if ([platform isEqualToStringAdd:@"iPhone6,2"]) return @"iPhone 5s (A1457/A1518/A1528/A1530)";
    if ([platform isEqualToStringAdd:@"iPhone7,1"]) return @"iPhone 6 Plus (A1522/A1524)";
    if ([platform isEqualToStringAdd:@"iPhone7,2"]) return @"iPhone 6 (A1549/A1586)";
    if ([platform isEqualToStringAdd:@"iPhone8,1"]) return @"iPhone6s";
    if ([platform isEqualToStringAdd:@"iPhone8,2"]) return @"iPhone6sPlus";
    if ([platform isEqualToStringAdd:@"iPhone8,3"]) return @"iPhoneSE";
    if ([platform isEqualToStringAdd:@"iPhone8,4"]) return @"iPhoneSE";
    if ([platform isEqualToStringAdd:@"iPhone9,1"]) return @"iPhone7";
    if ([platform isEqualToStringAdd:@"iPhone9,2"]) return @"iPhone7Plus";
    
    if ([platform isEqualToStringAdd:@"iPod1,1"])   return @"iPod Touch 1G (A1213)";
    if ([platform isEqualToStringAdd:@"iPod2,1"])   return @"iPod Touch 2G (A1288)";
    if ([platform isEqualToStringAdd:@"iPod3,1"])   return @"iPod Touch 3G (A1318)";
    if ([platform isEqualToStringAdd:@"iPod4,1"])   return @"iPod Touch 4G (A1367)";
    if ([platform isEqualToStringAdd:@"iPod5,1"])   return @"iPod Touch 5G (A1421/A1509)";
    
    if ([platform isEqualToStringAdd:@"iPad1,1"])   return @"iPad 1G (A1219/A1337)";
    
    if ([platform isEqualToStringAdd:@"iPad2,1"])   return @"iPad 2 (A1395)";
    if ([platform isEqualToStringAdd:@"iPad2,2"])   return @"iPad 2 (A1396)";
    if ([platform isEqualToStringAdd:@"iPad2,3"])   return @"iPad 2 (A1397)";
    if ([platform isEqualToStringAdd:@"iPad2,4"])   return @"iPad 2 (A1395+New Chip)";
    if ([platform isEqualToStringAdd:@"iPad2,5"])   return @"iPad Mini 1G (A1432)";
    if ([platform isEqualToStringAdd:@"iPad2,6"])   return @"iPad Mini 1G (A1454)";
    if ([platform isEqualToStringAdd:@"iPad2,7"])   return @"iPad Mini 1G (A1455)";
    
    if ([platform isEqualToStringAdd:@"iPad3,1"])   return @"iPad 3 (A1416)";
    if ([platform isEqualToStringAdd:@"iPad3,2"])   return @"iPad 3 (A1403)";
    if ([platform isEqualToStringAdd:@"iPad3,3"])   return @"iPad 3 (A1430)";
    if ([platform isEqualToStringAdd:@"iPad3,4"])   return @"iPad 4 (A1458)";
    if ([platform isEqualToStringAdd:@"iPad3,5"])   return @"iPad 4 (A1459)";
    if ([platform isEqualToStringAdd:@"iPad3,6"])   return @"iPad 4 (A1460)";
    
    if ([platform isEqualToStringAdd:@"iPad4,1"])   return @"iPad Air (A1474)";
    if ([platform isEqualToStringAdd:@"iPad4,2"])   return @"iPad Air (A1475)";
    if ([platform isEqualToStringAdd:@"iPad4,3"])   return @"iPad Air (A1476)";
    if ([platform isEqualToStringAdd:@"iPad4,4"])   return @"iPad Mini 2G (A1489)";
    if ([platform isEqualToStringAdd:@"iPad4,5"])   return @"iPad Mini 2G (A1490)";
    if ([platform isEqualToStringAdd:@"iPad4,6"])   return @"iPad Mini 2G (A1491)";
    
    if ([platform isEqualToStringAdd:@"i386"])      return @"iPhone Simulator";
    if ([platform isEqualToStringAdd:@"x86_64"])    return @"iPhone Simulator";
    return platform;
}

+(BOOL)checkNumber:(NSString *)string
{
    if ([self isEmpty:string]) {
        return NO;
    }
    NSCharacterSet*cs;
    cs =[[NSCharacterSet characterSetWithCharactersInString:NUMBERSPERIOD]invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
    BOOL basicTest = [string isEqualToStringAdd:filtered];
    return basicTest;
}

+(BOOL)checkMobile:(NSString *)_text
{
    if ([self isEmpty:_text]) {
        return NO;
    }
    NSString *Regex = @"1\\d{10}";
    NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    return [Test evaluateWithObject:_text];
}

//通过区分字符串

+(BOOL)validateEmail:(NSString*)email

{
    
    if((0 != [email rangeOfString:@"@"].length) &&
       
       (0 != [email rangeOfString:@"."].length))
        
    {
        
        NSCharacterSet* tmpInvalidCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
        
        NSMutableCharacterSet* tmpInvalidMutableCharSet = [tmpInvalidCharSet mutableCopy];
        
        [tmpInvalidMutableCharSet removeCharactersInString:@"_-"];
        
        
        
        /*
         
         *使用compare option 来设定比较规则，如
         
         *NSCaseInsensitiveSearch是不区分大小写
         
         *NSLiteralSearch 进行完全比较,区分大小写
         
         *NSNumericSearch 只比较定符串的个数，而不比较字符串的字面值
         
         */
        
        NSRange range1 = [email rangeOfString:@"@"
                          
                                      options:NSCaseInsensitiveSearch];
        
        
        
        //取得用户名部分
        
        NSString* userNameString = [email substringToIndex:range1.location];
        
        NSArray* userNameArray   = [userNameString componentsSeparatedByString:@"."];
        
        
        
        for(NSString* string in userNameArray)
            
        {
            
            NSRange rangeOfInavlidChars = [string rangeOfCharacterFromSet: tmpInvalidMutableCharSet];
            
            if(rangeOfInavlidChars.length != 0 || [string isEqualToStringAdd:@""])
                
                return NO;
            
        }
        
        
        
        //取得域名部分
        
        NSString *domainString = [email substringFromIndex:range1.location+1];
        
        NSArray *domainArray   = [domainString componentsSeparatedByString:@"."];
        
        
        
        for(NSString *string in domainArray)
            
        {
            
            NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:tmpInvalidMutableCharSet];
            
            if(rangeOfInavlidChars.length !=0 || [string isEqualToStringAdd:@""])
                
                return NO;
            
        }
        
        
        
        return YES;
        
    }
    
    else {
        
        return NO;
        
    }
    
}



//利用正则表达式验证

+(BOOL)isValidateEmail:(NSString *)email {
    
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 
    
    return [emailTest evaluateWithObject:email];
    
}
// 格式化金额
+(NSString*)formatterDecimalWithSum:(CGFloat)sum{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    return [formatter stringFromNumber:[NSNumber numberWithInt:sum]];
}

/**
 *  格式话小数 四舍五入类型
 *
 *  @param format @"0.00"
 *  @param floatV
 *
 *  @return 格式后的字符串
 */
+ (NSString *) decimalwithFormat:(NSString *)format  floatV:(float)floatV
{
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    
    [numberFormatter setPositiveFormat:format];
    
    return  [numberFormatter stringFromNumber:[NSNumber numberWithFloat:floatV]];
}

+(NSString*)moneyFormat:(id)money1
{
    NSString *money;
    if (![money1 isKindOfClass:[NSString class]]) {
        money = [NSString stringWithFormat:@"%@",money1];
    }else {
        money = money1;
    }
    if ([money isEqualToStringAdd:@""] || [money rangeOfString:@"null"].length>0 || money == NULL || [money isKindOfClass:[NSNull class]] ) {
        return @"0.00";
    }
    if ([money rangeOfString:@"null"].length > 0) {
        return @"0.00";
    }
    if ([money isEqualToStringAdd:@"0"]||[money isEqualToStringAdd:@"0.0"] || [money isEqualToStringAdd:@"0.00"] || [money isEqualToStringAdd:@"0."] ) {
        return @"0.00";
    }
    
    //    NSString *moneyzero;
    //    BOOL issubstring = NO;
    //    for (int i =0; i< money.length; i++) {
    //        if ([[money substringWithRange:NSMakeRange(i, 1)] intValue]==0) {
    //            moneyzero = [money substringFromIndex:i+1];
    //            issubstring = YES;
    //            if (i==money.length-1) {
    //                return @"0.00";
    //            }
    //        }else {
    //            break;
    //        }
    //    }
    //    if (issubstring) {
    //        money = moneyzero;
    //    }
    NSString *newMoney;
    NSString *dian = @".";
    NSRange range = [money rangeOfString:dian];
    NSInteger i = range.length;
    if (i == 0) {
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        [formatter setPositiveFormat:@"#,###,###.00"];
        newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
    }else {
        NSInteger l = [money length];
        NSInteger lo = range.location;
        if ((l-lo-1)==1) {
            if ([[money substringWithRange:NSMakeRange(lo+1, 1)] isEqualToStringAdd:@"0"] ) {
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                [formatter setPositiveFormat:@"#,###,###.00"];
                newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
                if (lo == 1) {
                    if ([[money substringToIndex:1] isEqualToStringAdd:@"0"]) {
                        newMoney = @"0.00";
                    }
                }
            }else {
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                [formatter setPositiveFormat:@"#,###,###.#"];
                newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
                newMoney =[NSString stringWithFormat:@"%@0",newMoney];
                if (lo == 1) {
                    if ([[money substringToIndex:1] isEqualToStringAdd:@"0"]) {
                        newMoney = [NSString stringWithFormat:@"0.%@0",[money substringWithRange:NSMakeRange(2, 1)]];
                    }
                }
            }
        }else if((l - lo - 1)== 0){
            NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
            [formatter setPositiveFormat:@"#,###,###."];
            newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
            newMoney =[NSString stringWithFormat:@"%@00",newMoney];
            
        }else {
            if ([[money substringWithRange:NSMakeRange(lo+1, 1)] isEqualToStringAdd:@"0"] && [[money substringWithRange:NSMakeRange(lo+2, 1)] isEqualToStringAdd:@"0"]) {
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                [formatter setPositiveFormat:@"#,###,###.00"];
                newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
                if (lo == 1) {
                    if ([[money substringToIndex:1] isEqualToStringAdd:@"0"]) {
                        newMoney = @"0.00";
                    }
                }
                
            }else if(![[money substringWithRange:NSMakeRange(lo+1, 1)] isEqualToStringAdd:@"0"] && [[money substringWithRange:NSMakeRange(lo+2, 1)] isEqualToStringAdd:@"0"]) {
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                [formatter setPositiveFormat:@"#,###,###.#"];
                newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
                newMoney = [NSString stringWithFormat:@"%@0",newMoney];
                if (lo == 1) {
                    if ([[money substringToIndex:1] isEqualToStringAdd:@"0"]) {
                        newMoney = [NSString stringWithFormat:@"0.%@0",[money substringWithRange:NSMakeRange(lo+1, 1)]];
                    }
                }
            }
            else {
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                [formatter setPositiveFormat:@"#,###,###.##"];
                newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]]; 
            }
            
        }
    }
    return newMoney;
}

+(NSString*)moneyFormat1:(id)money1
{
    NSString *money;
    if (![money1 isKindOfClass:[NSString class]]) {
        money = [NSString stringWithFormat:@"%@",money1];
    }else {
        money = money1;
    }
    if ([money isEqualToStringAdd:@""] || [money rangeOfString:@"null"].length>0 || money == NULL || [money isKindOfClass:[NSNull class]] ) {
        return @"0.00";
    }
    if ([money rangeOfString:@"null"].length > 0) {
        return @"0.00";
    }
    if ([money isEqualToStringAdd:@"0"]||[money isEqualToStringAdd:@"0.0"] || [money isEqualToStringAdd:@"0.00"] || [money isEqualToStringAdd:@"0."] ) {
        return @"0.00";
    }
    
    //    NSString *moneyzero;
    //    BOOL issubstring = NO;
    //    for (int i =0; i< money.length; i++) {
    //        if ([[money substringWithRange:NSMakeRange(i, 1)] intValue]==0) {
    //            moneyzero = [money substringFromIndex:i+1];
    //            issubstring = YES;
    //            if (i==money.length-1) {
    //                return @"0.00";
    //            }
    //        }else {
    //            break;
    //        }
    //    }
    //    if (issubstring) {
    //        money = moneyzero;
    //    }
    NSString *newMoney;
    NSString *dian = @".";
    NSRange range = [money rangeOfString:dian];
    NSInteger i = range.length;
    if (i == 0) {
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        [formatter setPositiveFormat:@"#,###,###"];
        newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
    }else {
        NSInteger l = [money length];
        NSInteger lo = range.location;
        if ((l-lo-1)==1) {
            if ([[money substringWithRange:NSMakeRange(lo+1, 1)] isEqualToStringAdd:@"0"] ) {
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                [formatter setPositiveFormat:@"#,###,###"];
                newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
                if (lo == 1) {
                    if ([[money substringToIndex:1] isEqualToStringAdd:@"0"]) {
                        newMoney = @"0.00";
                    }
                }
            }else {
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                [formatter setPositiveFormat:@"#,###,###.#"];
                newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
                newMoney =[NSString stringWithFormat:@"%@0",newMoney];
                if (lo == 1) {
                    if ([[money substringToIndex:1] isEqualToStringAdd:@"0"]) {
                        newMoney = [NSString stringWithFormat:@"0.%@0",[money substringWithRange:NSMakeRange(2, 1)]];
                    }
                }
            }
        }else if((l - lo - 1)== 0){
            NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
            [formatter setPositiveFormat:@"#,###,###."];
            newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
            newMoney =[NSString stringWithFormat:@"%@00",newMoney];
            
        }else {
            if ([[money substringWithRange:NSMakeRange(lo+1, 1)] isEqualToStringAdd:@"0"] && [[money substringWithRange:NSMakeRange(lo+2, 1)] isEqualToStringAdd:@"0"]) {
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                [formatter setPositiveFormat:@"#,###,###"];
                newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
                if (lo == 1) {
                    if ([[money substringToIndex:1] isEqualToStringAdd:@"0"]) {
                        newMoney = @"0.00";
                    }
                }
                
            }else if(![[money substringWithRange:NSMakeRange(lo+1, 1)] isEqualToStringAdd:@"0"] && [[money substringWithRange:NSMakeRange(lo+2, 1)] isEqualToStringAdd:@"0"]) {
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                [formatter setPositiveFormat:@"#,###,###.#"];
                newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
                newMoney = [NSString stringWithFormat:@"%@0",newMoney];
                if (lo == 1) {
                    if ([[money substringToIndex:1] isEqualToStringAdd:@"0"]) {
                        newMoney = [NSString stringWithFormat:@"0.%@0",[money substringWithRange:NSMakeRange(lo+1, 1)]];
                    }
                }
            }
            else {
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                [formatter setPositiveFormat:@"#,###,###.##"];
                newMoney = [formatter stringFromNumber:[NSNumber numberWithDouble:[money doubleValue]]];
            }
            
        }
    }
    return newMoney;
}

+ (int)compareCurrentTime:(NSDate *)compareDate
{
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    int temp = 0;
    int result;
    if((temp = timeInterval/60) <60){
        result = temp;
    }

    return  result;
}

//把手机号码中间四位用*代替
+ (NSString *)MobileFormat:(NSString *)mobile
{
    NSString *newMobile = @"";
    if ([mobile length] < 11) {
        return mobile;
    }
    NSRange rang = NSMakeRange(7, 4);
    newMobile = [newMobile stringByAppendingFormat:@"%@****%@",[mobile substringToIndex:3],[mobile substringWithRange:rang]];
    return newMobile;
}
/**
 *  @author 赵继申, 16-01-15 17:01:57
 *
 *  拨打电话
 *
 *  @param phoneNum 电话号码
 *  @param view     弹出拨打电话的对话框放在哪个view上
 */
+(void)callPhone:(NSString*)phoneNum view:(UIView*)view{
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phoneNum];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [view addSubview:callWebview];
}

/**
 *  @author 赵继申, 16-02-22 15:02:09
 *
 *  类名转String
 *
 *  @param classNameStr
 *
 *  @return
 */
+(NSString*)getClassNameStr:(NSObject*)object1{
    return [NSString stringWithUTF8String:object_getClassName(object1)];
}
/**
 *  @author zhao, 16-06-22 17:06:39
 *
 *  全角转半角
 *
 *  @param string <#string description#>
 *
 *  @return <#return value description#>
 */
+(NSString*)stringTransformFullwidthHalfwidth:(NSString *)string{
    NSMutableString *convertedString = [string mutableCopy];
    
    CFStringTransform((CFMutableStringRef)convertedString, NULL, kCFStringTransformFullwidthHalfwidth, false);

    return [NSString stringWithFormat:@"%@",convertedString];
}

+(UIViewController*)getCurrentVisibleViewController{
    UIViewController *vc = ((UINavigationController*)(((AppDelegate*)([UIApplication sharedApplication].delegate)).tabBarController).selectedViewController).visibleViewController;
    return vc;
}
@end
